/*
 Copyright (C) 2009 QDSS.org
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.qdss.persist.dialect.editor;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.lang.StringUtils;
import org.qdss.persist.dialect.FieldType;
import org.qdss.persist.engine.entry.ID;


/**
 * 
 * 
 * @author <a href="mailto:zhaofang123@gmail.com">FANGFANG ZHAO</a>
 * @since 0.1.0, Feb 12, 2009
 * @version $Id: PrimaryEditor.java 21 2009-02-12 15:44:18Z
 *          zhaofang123@gmail.com $
 */
public class PrimaryEditor extends StringEditor {

	public int getType() {
		return FieldType.PRIMARY.getMask();
	}

	@Override
	public void set(PreparedStatement pstmt, int index, Object value)
			throws SQLException {
		super.set(pstmt, index, ((ID) value).toLiteral());
	}
	
	@Override
	public Object get(ResultSet rs, int index) throws SQLException {
		String v = rs.getString(index);
		return (StringUtils.isEmpty(v)) ? null : ID.valueOf(v);
	}

	@Override
	public String toLiteral(Object value) {
		return (value == null) ? null : ((ID) value).toLiteral();
	}
}
